{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5e313815",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai # 导入OpenAI，用于调用聊天模型\n",
    "import IPython # 导入IPython，用于展示聊天输出\n",
    "\n",
    "# 这里替换为你的 OpenAI API 密钥\n",
    "openai.api_key  = 'sk-xxxxx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d48dc6dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置OpenAI API调用参数\n",
    "def set_open_params(\n",
    "#     model=\"text-davinci-003\", # 模型\n",
    "    model=\"gpt-4\", # 模型\n",
    "    temperature=0.7, # 多样性\n",
    "    n = 1, # 返回结果的数量\n",
    "    max_tokens=256, # 生成结果的最大词数\n",
    "    top_p=1, # 随机抽样的阈值\n",
    "    frequency_penalty=0, # 调整生成的文本的常见度\n",
    "    presence_penalty=0, # 调整生成的文本中的新概念出现的频率\n",
    "):\n",
    "    openai_params = {}    \n",
    "    openai_params['model'] = model  # 使用的模型名称\n",
    "    openai_params['temperature'] = temperature  # 结果多样性的参数，数值越大结果越随机，反之越确定\n",
    "    openai_params['n'] = n  # 返回结果的数量\n",
    "    openai_params['max_tokens'] = max_tokens  # 生成结果的最大词数\n",
    "    openai_params['top_p'] = top_p  # 随机抽样的阈值，较小的值将使结果更加集中，较大的值将使结果更加分散\n",
    "    openai_params['frequency_penalty'] = frequency_penalty  # 调整生成的文本的常见度，较高的值将使模型更可能生成罕见的文本\n",
    "    openai_params['presence_penalty'] = presence_penalty  # 调整生成的文本中的新概念出现的频率，较高的值将使模型更可能引入新的概念\n",
    "    return openai_params\n",
    "\n",
    "params = set_open_params() # 设置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d9167538",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从OpenAI API获取完成的文本\n",
    "def get_completion(params, prompt):\n",
    "    messages = [{\"role\": \"user\", \"content\": prompt}]  # 用户角色发送一个消息，消息的内容为我们的提示文本\n",
    "    response = openai.ChatCompletion.create(  # 调用ChatCompletion API\n",
    "        model = params['model'], # 模型\n",
    "        messages=messages, # Prompt消息\n",
    "        temperature = params['temperature'], # 多样性\n",
    "        n = params['n'], # 返回结果的数量\n",
    "        max_tokens = params['max_tokens'], # 生成结果的最大词数\n",
    "        top_p = params['top_p'], # 随机抽样的阈值\n",
    "        frequency_penalty = params['frequency_penalty'], # 生成的文本的常见度\n",
    "        presence_penalty = params['presence_penalty'], # 生成的文本中的新概念出现的频率\n",
    "    )\n",
    "    return response # 接收模型的聊天返回结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f38a18f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型的输出:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "1. 歌曲主题：这首歌是表达深深的爱意，用月亮来象征自己的心，向对方表达自己真挚的感情。\n",
       "2. 英文翻译：The theme of the song is to express deep love, symbolizing one's heart with the moon, expressing sincere feelings to the other party.\n",
       "3. 抽象词汇：theme, express, deep, love, symbolizing, heart, feelings, sincere, other party\n",
       "4. json对象：{\"English Summary\": \"The theme of the song is to express deep love, symbolizing one's heart with the moon, expressing sincere feelings to the other party.\", \"Number of Abstract Words\": 9}"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = f\"\"\"\n",
    "月亮代表我的心，\n",
    "你问我爱你有多深，\n",
    "我爱你有几分？\n",
    "我的情也真，\n",
    "我的爱也真，\n",
    "月亮代表我的心。\n",
    "\"\"\"\n",
    "prompt = f\"\"\"\n",
    "你的任务是：\n",
    "1. 用一句话总结<>中歌曲的主题。\n",
    "2. 把总结翻译成英文。\n",
    "3. 列出英文总结中所有的抽象词汇。\n",
    "4. 输出一个包含英文总结和抽象词汇数量的json对象。\n",
    "\n",
    "提示：先完成总结，再翻译，最后输出json。\n",
    "\n",
    "歌曲：<{text}>\n",
    "\"\"\"\n",
    "response = get_completion(params, prompt)\n",
    "print(\"模型的输出:\")\n",
    "IPython.display.Markdown(response.choices[0].message[\"content\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c8e58483",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型的输出:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "对不起，可能您的信息有误。胡适并非唐朝时期的文学家，而是现代著名的哲学家、历史学家、教育家、诗人，他出生于1891年，去世于1962年。他的主要学术贡献在于推动新文化运动，提倡白话文学，推动中国现代化的进程。\n",
       "\n",
       "至于您提到的《朝花夕拾》，这是鲁迅的散文集，收录了他在1926年至1935年间发表的散文。这些散文以对社会现象和历史事件的独特见解为特点，语言风格犀利，对中国社会的批评犀利，反映了他的激进思想和深沉的忧国忧民之情。\n",
       "\n",
       "如果您需要了解关于胡适的"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "唐朝时期，著名的文学家胡适写了一本传奇小说《朝花夕拾》，\n",
    "请介绍一下这本书的详细内容，人物以及里面的精彩故事片段。\n",
    "\"\"\"\n",
    "response = get_completion(params, prompt)\n",
    "print(\"模型的输出:\")\n",
    "IPython.display.Markdown(response.choices[0].message[\"content\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "377d79ed",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3de54c85",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
